package cn.zhoujing.security.demo.common.handler;

import cn.zhoujing.security.demo.common.api.CommonResult;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

/**
 * 认证异常处理器
 *
 * @author zhoujing
 * @createTime 2024/2/8 - 2:01
 */
@Slf4j
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        System.err.println("[MyAuthenticationEntryPoint]："+authException.toString());
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().print(JSON.toJSONString(CommonResult.unauthorized(null)));
        response.getWriter().flush();
    }


}
